<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:09
 */

namespace App\Admin\Controllers;

use App\Admin\Contracts\AuthRuleInterface;
use App\Admin\Contracts\MemberInterface;
use App\Base\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 用户控制类
 * Class MenuController
 * @package App\Admin\Controllers
 */
class MemberController extends Controller
{
    /**
     * 用户接口
     * @return MemberInterface
     */
    public function getMemberInterface()
    {
        return app()->make(MemberInterface::class);
    }

    /**
     * 权限接口
     * @return AuthRuleInterface
     */
    public function getAuthRuleInterface(){
        return app()->make(AuthRuleInterface::class);
    }

    /**
     * 用户列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        if ($request->getMethod() == "POST") {


            $where = function ($query) use ($request) { // 查询搜索
                $start_date = $request->input("start_date", "");
                if (!empty($start_date)) {
                    $query->where("update_time", ">", strtotime($start_date));
                }
                $end_date = $request->input("end_date", "");
                if (!empty($end_date)) {
                    $query->where("update_time", "<", strtotime($end_date));
                }
                $account = $request->post("account", "");
                if (!empty($account)) {
                    $uid = $this->getMemberInterface()->getUidByAccount($account);
                    $query->where("uid", $uid);
                }
                $nickname = $request->input("nickname"); // 标题
                if (!empty($nickname)) {
                    $query->where("nickname", "like", '%' . $nickname . '%');
                }
            };
            $memberList = $this->getMemberInterface()->getMemberList($where);

            foreach ($memberList as $k => $v) {

                $memberList[$k]->account = $this->getMemberInterface()->getAccountByUid($v->uid);
            }

            return $memberList;
        }
        return view("admin.member.index");
    }

    public function edit(Request $request)
    {

        if ($request->getMethod() == "POST") {

            $validator = Validator::make($request->all(), [
                'id' => 'required',
            ], ["id.required" => '用户名是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->getMemberInterface()->updateMemberInfoRow($request->all());
        }
        $uid = $request->route('uid');
        /** @var TYPE_NAME $where */
        $where = function ($query) use ($uid) {
            if (!empty($uid)) {
                $query->where("uid", $uid);
            }
        };
        $data = $this->getMemberInterface()->getMemberInfo($where);
        $udata = $this->getMemberInterface()->getUcnterMemberInfo(["id"=>$uid]);
        $newdata=array_merge($data->toArray(),$udata->toArray());
        return view("admin.member.edit")->with("data", $newdata);
    }

    /**
     * 用户选择角色
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function changeRole(Request $request){

        if ($request->getMethod() == "POST") {
            $validator = Validator::make($request->all(), [
                'uid' => 'required',
            ], ["uid.required" => '用户名是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $this->getAuthRuleInterface()->delUserAuthGroupAccess(["uid"=>$request->input("uid")]);
            return $this->getAuthRuleInterface()->saveUserAuthGroupAccess($request->all());
        }
        $groupRoleList=$this->getAuthRuleInterface()->getAuthGroupList([]);

        $uid = $request->input('uid');

        $group_id=$this->getAuthRuleInterface()->getUserAuthGroupAccessGroupId(["uid"=>$uid]);

        /** @var TYPE_NAME $where */
        $where = function ($query) use ($uid) {
            if (!empty($uid)) {
                $query->where("uid", $uid);
            }
        };
        $data = $this->getMemberInterface()->getMemberInfo($where);
        $udata = $this->getMemberInterface()->getUcnterMemberInfo(["id"=>$uid]);
        $newdata=array_merge($data->toArray(),$udata->toArray());
      return view("admin.member.change_role")->with("data",$newdata)->with("groupRoleList",$groupRoleList)->with("group_id",$group_id);
    }

    /**
     * 用户删除
     * @param Request $request
     */
    public function del(Request $request)
    {
        return $this->getMemberInterface()->delMember($request->all());
    }

    /**
     * 重置google邦定
     * @param Request $request
     */
    public function optgg(Request $request)
    {
      return $this->getMemberInterface()->updateMember($request->all(),["otp_gg"=>'']);
    }

    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->getMethod() == "POST") {
           return  $this->getMemberInterface()->addMemberInfoRow($request->all());
        }
        return view("admin.member.add");
    }

    /**
     * 用户修改密码
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function password(Request $request){
        if($request->getMethod()=="POST"){
            return  $this->getMemberInterface()->updatePassword($request->all());
        }
        return view("admin.member.password");
    }

    /**
     * 用户基本信息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function info(Request $request){
        if($request->getMethod()=="POST"){
            return $this->getMemberInterface()->updateMember($request->all());
        }
        $data = $this->getMemberInterface()->getMemberInfo(['uid'=>UID]);
        $udata = $this->getMemberInterface()->getUcnterMemberInfo(["id"=>UID]);
        $newdata=array_merge($data->toArray(),$udata->toArray());
        return view("admin.member.info")->with("data",$newdata);
    }

    /**
     * 修改用户状态
     * @param Request $request
     * @return mixed
     */
    public function changeStatus(Request $request){
        return $this->getMemberInterface()->changeStatus($request->all());
    }



}